package org.online.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.online.conmon.Const;
import org.online.entity.User;

@WebServlet("/index.html")
public class IndexServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		PrintWriter out = resp.getWriter();

		// 获取会话对象
		HttpSession session = req.getSession();

		// 从会话中，获取登录帐号
		User user = (User) session.getAttribute(Const.LOGIN_USER);

		// 获取上下文对象
		ServletContext context = req.getServletContext();

		// 在上下文对象中，获取map集合
		@SuppressWarnings("unchecked")
		Map<String, User> map = (Map<String, User>) context.getAttribute(Const.ONLINE_USER_MAP);

		// 动态显示数据 - HTML - JSP
		out.print("<!DOCTYPE html>");
		out.print("<html>");
		out.print("<head>");
		out.print("<meta charset='UTF-8'>");
		out.print("<title>Insert title here</title>");
		out.print("</head>");
		out.print("<body>");
		out.print("<h1>欢迎进入后台管理系统</h1>");
		out.print("<h3>您好，" + user.getUsername() + "</h3>");
		out.print("<h5>当前在线人数：" + map.size() + "  &nbsp;&nbsp;  <a href='listOnineUser.do'>查看在线用户</a>  &nbsp;&nbsp;  <a href='logout.do'>退出登录</a></h5>");
		out.print("<hr/>");
		out.print("</body>");
		out.print("</html>");

		// 关闭对象
		out.flush();
		out.close();

	}

}
